﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;

namespace VPLink2013.Framework.Extensions
{
    public static partial class FrameworkExtensions
    {
        public static bool IsNullOrEmpty<T>(this List<T> input)
        {
            try
            {
                if (input == null)
                {
                    return true;
                }

                if (input.Count == 0)
                {
                    return true;
                }

                return false;
            }
            catch (Exception)
            {
                //TODO : log
                return false;
            }
        }

        public static bool IsNotNullOrEmpty<T>(this List<T> input)
        {
            return input.IsNullOrEmpty() == false;
        }

        public static bool SafeContains(this List<string> input, string stringToCompare, StringComparison stringComparison)
        {
            try
            {
                if (input.IsNullOrEmpty())
                {
                    return false;
                }

                foreach (var item in input)
                {
                    if (item.SafeEquals(stringToCompare,stringComparison))
                    {
                        return true;
                    }
                }

                return false;
                
            }
            catch (Exception)
            {
                //TODO : log
                return false;
            }
        }

        public static string ToStringWithSeparator(this List<string> input, string separator)
        {
            try
            {
                if (input.IsNullOrEmpty())
                {
                    return null;
                }

                if (separator.IsNull())
                {
                    separator = string.Empty;
                }

                if (input.Count == 1)
                {
                    return input[0];
                }

                StringBuilder sb = new StringBuilder();
                int maxIndexValue = input.Count - 1;
                int i = 0;
                foreach (var item in input)
                {
                    sb.Append(item);
                    if (i < maxIndexValue)
                    {
                        sb.Append(separator);
                    }
                    i += 1;
                }

                string result = sb.ToString();
                return result;

            }
            catch (Exception)
            {
                //TODO : log
                return null;
            }
        }

        public static List<string> RemoveDuplicateEntries(this List<string> input)
        {
            try
            {
                if (input.IsNullOrEmpty())
                {
                    return null;
                }

                List<string> noDuplicateList = new List<string>();
                foreach (var item in input)
                {
                    if (noDuplicateList.SafeContains(item,StringComparison.CurrentCultureIgnoreCase))
                    {
                        continue;
                    }
                    noDuplicateList.Add(item);
                }

                return noDuplicateList;
            }
            catch (Exception)
            {
                //TODO : log
                return input;
            }
        }


        public static bool HasOneItemIn(this List<string> input, List<string> items)
        {
            try
            {
                if (items.IsNullOrEmpty())
                {
                    return false;
                }

                if (input.IsNullOrEmpty())
                {
                    return false;
                }

                foreach (var item in input)
                {
                    if (items.SafeContains(item, StringComparison.CurrentCultureIgnoreCase))
                    {
                        return true;
                    }
                }

                return false;
            }
            catch (Exception ex)
            {
                //TODO : log
                return false;
            }
        }

        public static string ToJson(this List<string> input)
        {
            try
            {
                if (input.IsNullOrEmpty())
                {
                    return "[]";
                }

                StringBuilder sb = new StringBuilder();
                sb.AppendLine("[");

                int itemCount = input.Count;
                int i = 1;
                foreach (var item in input)
                {
                    string jsonItem = string.Format(@" ""{0}"" ", item.ToJson());
                    sb.Append(jsonItem);
                    if (i < itemCount)
                    {
                        sb.AppendLine(",");
                    }
                    i += 1;
                }

                sb.AppendLine("]");
                return sb.ToString();

            }
            catch (Exception ex)
            {
                //TODO : log
                return "[]";
            }
        }

        public static string ToJson<T>(this List<T> input) where T:class
        {
            try
            {
                if (input.IsNullOrEmpty())
                {
                    return "[]";
                }

                StringBuilder sb = new StringBuilder();
                sb.AppendLine("[");

                int itemCount = input.Count;
                int i = 1;
                foreach (var item in input)
                {
                    string jsonItem = item.ToJson<T>();
                    sb.Append(jsonItem);
                    if (i < itemCount)
                    {
                        sb.AppendLine(",");
                    }
                    i += 1;
                }

                sb.AppendLine("]");
                return sb.ToString();

            }
            catch (Exception ex)
            {
                //TODO : log
                return "[]";
            }
        }

        public static string[] ToSafeArray(this List<string> input)
        {
            try
            {
                if (input.IsNullOrEmpty())
                {
                    return null;
                }

                return input.ToArray();
            }
            catch (Exception)
            {
                //TODO : log
                return null;
            }
        }

        public static string ToXML<T>(this List<T> input) where T : class
        {
            try
            {
                if (input.IsNullOrEmpty())
                {
                    return "";
                }

                DataContractSerializer serializer = new DataContractSerializer(typeof(List<T>));
                using (MemoryStream stream = new MemoryStream())
                {
                    serializer.WriteObject(stream, input);
                    return Encoding.UTF8.GetString(stream.ToArray());
                }

            }
            catch (Exception ex)
            {
                //TODO : log
                return "";
            }
        }

    }
}
